package com.alog.yzd.bookmall.web.servlet.cart;

import com.alog.yzd.bookmall.domain.book.Book;
import com.alog.yzd.bookmall.domain.cart.CartItem;
import com.alog.yzd.bookmall.domain.user.User;
import com.alog.yzd.bookmall.service.cart.CartService;
import com.alog.yzd.commons.CommonUtils;
import com.alog.yzd.commons.Print;
import com.alog.yzd.commons.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class CartServlet extends BaseServlet {
    private CartService cartService = new CartService();

    /**
     * 加载多个CartItem
     */
    public String loadCartItems(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cartItemIds = req.getParameter("cartItemIds");
        List<CartItem> cartItems = cartService.loadCartItems(cartItemIds);
        req.setAttribute("cartItems", cartItems);
        Double total = Double.parseDouble(req.getParameter("hiddenTotal"));
        req.setAttribute("hiddenTotal", total);
        req.setAttribute("cartItemIds", cartItemIds);
        return "f:/jsps/cart/showitem.jsp";
    }

    /**
     * 修改条目的数量
     */
    public String updateQuantity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cartItemId = req.getParameter("cartItemId");
        String quantity = req.getParameter("quantity");
        CartItem item = cartService.updateQuantity(cartItemId, quantity);
        StringBuilder sb = new StringBuilder("{");
        sb.append("\"quantity\"").append(":").append(item.getQuantity());
        sb.append(",");
        sb.append("\"subtotal\"").append(":").append(item.getSubtotal());
        sb.append("}");
        Print.print("updateQuantity" + sb.toString());
        resp.getWriter().print(sb.toString());
        return null;
    }
    /**
     * 批量删除购物车中的条目
     */
    public String batchDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cartItemIds = req.getParameter("cartItemIds");
        cartService.batchDelete(cartItemIds);
        return myCart(req, resp);
    }
    /**
     * 添加购物车条目
     */
    public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CartItem cartItem = CommonUtils.toBean(req.getParameterMap(), CartItem.class);
        Book book = CommonUtils.toBean(req.getParameterMap(), Book.class);
        cartItem.setBook(book);
        User user = (User) req.getSession().getAttribute("sessionUser");
        cartItem.setUser(user);
        cartService.add(cartItem);
        return myCart(req, resp);
    }

    /**
     *我的购物车
     */
    public String myCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("sessionUser");
        String uid = user.getUid();
        List<CartItem> cartItems = cartService.myCart(uid);
        req.setAttribute("cartItemList", cartItems);
        return "f:/jsps/cart/list.jsp";
    }
}
